package com.coding.mapper;

import com.coding.entity.Conversation;
import com.coding.entity.vo.SessionVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Cynicism
 * @version 1.0
 * @project DyApp
 * @description
 * @date 2024/7/3 09:19:14
 */
@Mapper
public interface ConversationMapper {
    /**
     * 插入会话
     * @param userId1
     * @param userId2
     * @return
     */
    @Insert("INSERT INTO conversation (user_id1, user_id2) VALUES (#{userId1}, #{userId2})")
    Integer insertConversation(@Param("userId1") Long userId1, @Param("userId2") Long userId2);

    /**
     * 获取会话列表
     * @param userId
     * @return
     */
    @Select("SELECT conversation_id, user_id2, update_time FROM conversation WHERE user_id1 = #{userId} ")
    List<Conversation> getConversationList(@Param("userId") Long userId);

    /**
     * 是否存在用户会话，只要记录有userId1和userId2，就存在会话
     */
    @Select(" SELECT COUNT(*) FROM conversation WHERE (user_id1 = #{userId1} AND user_id2 = #{userId2}) OR (user_id1 = #{userId2} AND user_id2 = #{userId1})")
    Integer existsByUserIds(@Param("userId1") Long userId1, @Param("userId2") Long userId2);

}
